﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class HK_UserRightsList2 : ExtBaseRpt
{
    string mid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BinUserList("");
        }
    }
    protected void Getdata(object sender, DirectEventArgs e)
    {
        BinUserList(this.txtuser.Text.Trim());
        this.Store1.SetData(new object { });
        this.Store3.SetData(new object { });
        this.Store4.SetData(new object { });
        this.Store5.SetData(new object { });
    }
    protected void Cell_Click(object sender, DirectEventArgs e)
    {
        //加载功能模块和操作权限
        Getdata(null,null);
        RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        string sql = @"exec KM_GetUserRightsList '"+sm.SelectedRecordID+"','"+this.txtmode.Text+"'";
        this.Store1.DataSource = db.DM_GetDateSet(sql);
        this.Store1.DataBind();
        //加载用户角色
        sql = @"select r.RoleName from Sys_User_Role a
                inner join Sys_Role r on r.RoleID = a.RoleID
                where a.UserID = '"+sm.SelectedRecordID+"'";
        this.Store3.DataSource = db.DM_GetDateSet(sql);
        this.Store3.DataBind();
    }
    protected void Cell_Click2(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        string sql = @"select d.DataItemID,d.DataItemName,d.TableName,d.KeyFieldName from Sys_Module_Data a
                inner join Sys_Module m on m.ModuleCode = a.ModuleCode
                inner join Sys_DataItem d on d.DataItemID = a.DataItemID
                where m.ModuleCode='"+sm.SelectedRecordID+"'";
        this.Store4.DataSource = db.DM_GetDateSet(sql);
        this.Store4.DataBind();
    }
    protected void Cell_Click3(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        RowSelectionModel sm2 = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        RowSelectionModel sm3 = this.GridPanel4.GetSelectionModel() as RowSelectionModel;
        //获取数据权限关键内容

        string sql = "select *from Sys_DataItem where DataItemID = '" + sm3.SelectedRecordID + "'";
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                string tablename = ds.Tables[0].Rows[0]["TableName"].ToString();
                string keyname = ds.Tables[0].Rows[0]["KeyFieldName"].ToString();
                string showname = ds.Tables[0].Rows[0]["ShowFieldName"].ToString();
                sql = @"select distinct name from (
                            select " + showname + @" as name from " + tablename + @" a 
                            inner join Sys_RightRole_Data b on b.DataItemValue = a." + keyname + @"
                            inner join Sys_User_Role r on r.RoleID = b.RoleID
                            where b.DataItemID='" + sm3.SelectedRecordID + @"' and r.UserId ='" + sm2.SelectedRecordID + @"'
                            union all  
                            select " + showname + @" as name from " + tablename + @" a 
                            inner join Sys_RightRole_Data b on b.DataItemValue = a." + keyname + @"
                            inner join Sys_User u on u.userID = SUBSTRING(RoleID,3,len(RoleID))
                            where b.DataItemID='" + sm3.SelectedRecordID + @"' and u.UserId ='" + sm2.SelectedRecordID + @"'
                        )m";
            }
        }
        
        this.Store5.DataSource = db.DM_GetDateSet(sql);
        this.Store5.DataBind();
    }
    protected void MyData_Refresh(object sender, StoreReadDataEventArgs e)
    {
        BinUserList(this.txtuser.Text);
    }

    public void BinUserList(string param)
    {
        string str = "";
        if (param != "")
        {
            if (param == "%")
            {
                param = "";
            }
            str += " and (a.usercode like '%" + param + "%' or username like '%" + param
                + "%' or d.DepartMentName like '%" + param + @"%' or UserID in (select a.UserID from Sys_User_Role a 
            inner join Sys_Role r on r.RoleID = a.RoleID where r.RoleName like '%" + param + "%'))";
        }
        string sql = @"select a.UserID,a.UserCode,a.UserName,d.DepartMentName from Sys_User a
                        inner join Bas_Personnel b on b.PersonnelID = a.PersonnelID
                        inner join Bas_DepartMent d on d.DepartMentID = b.DepartMentID
                        where a.AllowUsed = 1 and d.FullParentID like (select '%'+DepartMentID+'%' 
                        from Bas_DepartMent where DepartMentName like '%信息中心%' and CompanyID='km') " + str;

        this.Store2.DataSource = db.DM_GetDateSet(sql);
        this.Store2.DataBind();
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void Edit(int mid, int userid, string field, string oldValue, string newValue, object customer)
    {
        //string message = "<b>Property:</b> {0}<br /><b>Field:</b> {1}<br /><b>Old Value:</b> {2}<br /><b>New Value:</b> {3}";
        // Send Message...
        //X.Msg.Notify(new NotificationConfig()
        //{
        //    Title = "Edit Record #" + id.ToString(),
        //    Html = string.Format(message, id, field, oldValue, newValue),
        //    Width = 250
        //}).Show();
        //更新到数据库
        string sql = "";
        sql = "update Sys_ModuleRights set " + field + "=" + (newValue.ToLower() == "true" ? 1 : 0)
           + " where mid=" + mid + " and userid=" + userid;
        //X.Msg.Alert("Submit", "You have submitted " + sql + " nodes").Show();
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            this.GridPanel1.GetStore().GetById(mid).Commit();
        }
        else
        {
            X.Msg.Notify(new NotificationConfig()
            {
                Title = "消息提示",
                Html = "修改失败！",
                Width = 250
            }).Show();
        }
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, this.Title);
    }
}